package com.rc.rc_exam.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rc.rc_exam.entity.PageResult;
import com.rc.rc_exam.entity.RCExam;
import com.rc.rc_exam.entity.RCExamTest;
import com.rc.rc_exam.entity.RCExamTestQuestion;
import com.rc.rc_exam.mapper.RCExamMapper;
import com.rc.rc_exam.service.RCExamService;
import com.rc.rc_exam.service.RCExamTestQuestionService;
import com.rc.rc_exam.service.RCExamTestService;
import com.rc.rc_exam.utils.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RCExamServiceImpl extends ServiceImpl<RCExamMapper, RCExam> implements RCExamService {

    @Autowired
    private RCExamMapper rcExamMapper;

    @Autowired
    private RCExamTestService rcExamTestService;

    @Autowired
    private RCExamTestQuestionService rcExamTestQuestionService;
    @Override
    public String createRcExamDataByExcel(MultipartFile file) {
        List<Map<String, String>> excelResults = ExcelUtil.readExcel(file);
        List<RCExam> rcExamList = new ArrayList<>();
        RCExam rcExam = null;
        for (Map<String,String> map : excelResults){
            rcExam = new RCExam();
            rcExam.setQuestion(map.get("题目内容"));
            rcExam.setOptionA(map.get("可选项A"));
            rcExam.setOptionB(map.get("可选项B"));
            rcExam.setOptionC(map.get("可选项C"));
            rcExam.setOptionD(map.get("可选项D"));
            rcExam.setOptionE(map.get("可选项E"));
            rcExam.setOptionF(map.get("可选项F"));
            rcExam.setOptionG(map.get("可选项G"));
            rcExam.setOptionH(map.get("可选项H"));
            rcExam.setOptionI(map.get("可选项I"));
            rcExam.setAnswer(map.get("正确答案").toUpperCase());
            rcExam.setQuestionType(map.get("题型"));
            rcExam.setQuestionValue(map.get("分数"));
            rcExam.setKnowPoint(map.get("知识点"));
            rcExam.setDifficulty(map.get("难度"));
            rcExamList.add(rcExam);
        }
        this.saveBatch(rcExamList);
        return "导入成功";
    }

    /**
     * 随机查询一百道题
     * @return
     */
    @Override
    public List<RCExam> findExamByPage(int num) {
        if (num == 0){
            num = 100;
        }
        long count = this.count();
        int lastPage = new Random().nextInt((int) count);
        int page = lastPage - num;
        Page<RCExam> rcExamPage = Page.of(page, num);
        Page<RCExam> examPage = rcExamMapper.selectPage(rcExamPage,new QueryWrapper<>());
        return examPage.getRecords();
    }

    @Override
    @Transactional
    public String saveTestRecord(Map<String, Object> paramMap) {
        String questionsStr = String.valueOf(paramMap.get("questions"));
        String score = String.valueOf(paramMap.get("score"));
        String testTime = String.valueOf(paramMap.get("testTime"));
        String testType = String.valueOf(paramMap.get("testType"));
        String currentNum = String.valueOf(paramMap.get("currentNum"));
        String num = String.valueOf(paramMap.get("num"));
        RCExamTest rcExamTest = new RCExamTest();
        rcExamTest.setTestName(testType+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        rcExamTest.setTestTime(testTime);
        rcExamTest.setScore(score);
        rcExamTest.setTestType(testType);
        if (StringUtils.isNotBlank(currentNum)){
            rcExamTest.setCurrentNum(currentNum);
        }
        if (StringUtils.isNotBlank(num)){
            rcExamTest.setNum(num);
        }
        rcExamTest.setLastNum(String.valueOf(((StringUtils.isBlank(currentNum)?0:Integer.valueOf(currentNum)) + (StringUtils.isBlank(num)? 0 :Integer.valueOf(num)))));
        long count = rcExamTestService.count();
        rcExamTest.setOrderNum(String.valueOf(count+1));
        rcExamTestService.save(rcExamTest);

        List<RCExam> rcExamList = JSONArray.parseArray(questionsStr, RCExam.class);
        List<RCExamTestQuestion> testQuestionList = new ArrayList<>();
        RCExamTestQuestion testQuestion = null;
        for (RCExam exam : rcExamList){
            testQuestion = new RCExamTestQuestion();
            testQuestion.setTestId(rcExamTest.getId());
            testQuestion.setQuestion(exam.getQuestion());
            testQuestion.setOptionA(exam.getOptionA());
            testQuestion.setOptionB(exam.getOptionB());
            testQuestion.setOptionC(exam.getOptionC());
            testQuestion.setOptionD(exam.getOptionD());
            testQuestion.setOptionE(exam.getOptionE());
            testQuestion.setOptionF(exam.getOptionF());
            testQuestion.setOptionG(exam.getOptionG());
            testQuestion.setOptionH(exam.getOptionH());
            testQuestion.setAnswer(exam.getAnswer());
            testQuestion.setSelectAnswer(exam.getSelectAnswer().substring(0,1));
            testQuestionList.add(testQuestion);
        }
        rcExamTestQuestionService.saveBatch(testQuestionList);
        return "保存成功";
    }

    /**
     * 获取顺序题目
     * @param currentNum
     * @param num
     * @return
     */
    @Override
    public List<RCExam> findSequenceExamByPage(int currentNum, int num) {
        Page<RCExam> rcExamPage = Page.of(currentNum, num);
        Page<RCExam> examPage = rcExamMapper.selectPage(rcExamPage, new QueryWrapper<>());
        return examPage.getRecords();
    }

    @Override
    public PageResult<RCExam> findExamsByPage(Map<String, Object> paramMap) {
        long currentPage = (long) paramMap.get("currentPage");
        long pageSize = (long) paramMap.get("pageSize");
        String examName = (String) paramMap.get("examName");
        Page<RCExam> rcExamPage = Page.of(currentPage, pageSize);
        QueryWrapper<RCExam> rcExamQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(examName)){
            rcExamQueryWrapper.like("exam_name",examName);
        }
        Page<RCExam> examPage = rcExamMapper.selectPage(rcExamPage, rcExamQueryWrapper);
        PageResult<RCExam> rcExamPageResult = new PageResult<RCExam>(examPage.getRecords(), examPage.getTotal());
        return rcExamPageResult;
    }
}
